package com.zxc.service;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import com.zxc.dao.TableInfoDAO;
import com.zxc.domain.TableHeader;
import com.zxc.domain.TableInfo;
import com.zxc.util.OracleDBUtil;

public class TableInfoService {
	private TableInfoDAO dao = new TableInfoDAO();
	
	public TableInfo getTableInfo(TableInfo tableInfo) throws SQLException, NoSuchMethodException, InvocationTargetException, IllegalAccessException{
		return dao.getTableInfo(tableInfo, OracleDBUtil.getConnection());
//		return dao.getTableInfo("select * from " + tableInfo.getTableSpecName(),tableInfo, OracleDBUtil.getConnection());
	}
	
	public String getTableHeaders(TableInfo tableInfo , String tag) throws SQLException{

		List<TableHeader> headers = tableInfo.getTableHeaders();
		StringBuffer sbuf = new StringBuffer();
		for(TableHeader temp : headers){
			sbuf.append(temp.getHeaderName()+tag);
		}
		return sbuf.length()-1 > 0 ?sbuf.substring(0,sbuf.length()-1) : sbuf.toString();
	}
	
	public String getTableTypes(TableInfo tableInfo , String tag) throws SQLException{
		List<TableHeader> headers = tableInfo.getTableHeaders();		
		StringBuffer sbuf = new StringBuffer();
		for(TableHeader temp : headers){
			if(!temp.getHeaderType().equals("NUMBER")){
				sbuf.append("string"+tag);
			}
			
			if(temp.getHeaderType().equals("NUMBER")){
				sbuf.append("double"+tag);
			}
		}
		return sbuf.length()-1 > 0 ?sbuf.substring(0,sbuf.length()-1) : sbuf.toString();
	}
	
	public String getTableComments(TableInfo tableInfo , String tag) throws SQLException{
		List<TableHeader> headers = tableInfo.getTableHeaders();	
//		String[] comments = tableInfo.getTableComments();
		StringBuffer sbuf = new StringBuffer();
		
		for(TableHeader temp : headers){			
			sbuf.append(temp.getComment()+tag);
		}
		
		return sbuf.length()-1 > 0 ?sbuf.substring(0,sbuf.length()-1) : sbuf.toString();
	}
	
	public String getTableFormat(TableInfo tableInfo , String tag) throws SQLException{
		List<TableHeader> headers = tableInfo.getTableHeaders();		
		StringBuffer sbuf = new StringBuffer();
		for(TableHeader temp : headers){
			if(!temp.getHeaderType().equals("NUMBER")){
				sbuf.append("-"+tag);
				continue;
			}
			
			if(temp.getHeaderType().equals("NUMBER")){
				if(temp.getNum()==null){
					sbuf.append("###"+tag);
				}else{
					sbuf.append("##0.00"+tag);
				}
				
			}
		}
		return sbuf.length()-1 > 0 ?sbuf.substring(0,sbuf.length()-1) : sbuf.toString();
	}
}
